package vip.xiaonuo.smzq.modular.ckq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.smzq.modular.ckq.entity.CkqCkssdj;
import vip.xiaonuo.smzq.modular.ckq.entity.vo.CkqVo;
import vip.xiaonuo.smzq.modular.ckq.mapper.CkqCkssdjMapper;
import vip.xiaonuo.smzq.modular.ckq.service.ICkqCkssdjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb08Xzqh;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb08XzqhMapper;
import vip.xiaonuo.smzq.modular.tkq.entity.vo.TkqVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 采矿申请登记 服务实现类
 *
 * @author sqq
 * @since 2024-05-23
 */
@Service
public class CkqCkssdjServiceImpl extends ServiceImpl<CkqCkssdjMapper, CkqCkssdj> implements ICkqCkssdjService {

    @Resource
    private CkqCkssdjMapper ckqCkssdjMapper;
    @Resource
    private Cb08XzqhMapper cb08XzqhMapper;//行政区划

    @Override
    public Page<CkqCkssdj> page(CkqCkssdj ckqCkssdj) {
        QueryWrapper<CkqCkssdj> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public Page<CkqVo> getCkqVoPage(long current, long size, String nd, String xzqh, String bglx) {
        Page<CkqCkssdj> page = new Page<>(current, size);
        QueryWrapper<CkqCkssdj> queryWrapper = new QueryWrapper<>();

        if (nd != null) {
            queryWrapper.like("qfsj", nd);
        }
        if (xzqh != null) {
            xzqh = xzqh.replaceAll("0+$", "");
            queryWrapper.like("szxzq", xzqh);
        }

        if (bglx != null) {

        }

        page= ckqCkssdjMapper.selectPage(page, queryWrapper);

        List<CkqCkssdj> records = page.getRecords();
        List<CkqVo> ckqVos = new ArrayList<>();

        for (CkqCkssdj record : records){
            CkqVo ckqVo = new CkqVo();
            BeanUtil.copyProperties(record, ckqVo);

            if (record.getSzxzq()!=null&&!record.getSzxzq().equals("0")&&!record.getSzxzq().equals("")) {
                //县
                QueryWrapper<Cb08Xzqh> xianWrapper = new QueryWrapper<>();
                xianWrapper.eq("dm", Integer.parseInt(record.getSzxzq()));
                Cb08Xzqh xian = cb08XzqhMapper.selectOne(xianWrapper);
                if (xian != null){
                    ckqVo.setXianxzqhName(xian.getMc());
                    //市
                    QueryWrapper<Cb08Xzqh> shiWrapper = new QueryWrapper<>();
                  String  xzqhDm = record.getSzxzq().replaceAll("0+$", "");
                    if (xzqhDm.length()<=4){
                        shiWrapper.eq("dm", xian.getDm());
                    }else{
                        shiWrapper.eq("dm", xian.getFdm());
                    }
                    Cb08Xzqh shi = cb08XzqhMapper.selectOne(shiWrapper);
                    ckqVo.setShixzqhName(shi.getMc());
                    //省
                    QueryWrapper<Cb08Xzqh> shengWrapper = new QueryWrapper<>();
                    shengWrapper.eq("dm", shi.getFdm());
                    Cb08Xzqh sheng = cb08XzqhMapper.selectOne(shengWrapper);
                    ckqVo.setShengxzqhName(sheng.getMc());
                }

            }
            ckqVos.add(ckqVo);
        }

        Page<CkqVo> tkqVoPage = new Page<>();
        tkqVoPage.setRecords(ckqVos);
        tkqVoPage.setCurrent(page.getCurrent());
        tkqVoPage.setSize(page.getSize());
        tkqVoPage.setTotal(page.getTotal());



        return tkqVoPage;
    }
}
